<template>
  <div id="userRegisterPage">
    <h1 style="margin-bottom: 16px">用户注册</h1>
    <a-form
      :model="form"
      style="max-width: 480px; margin: 0 auto"
      auto-label-width
      label-align="left"
      @submit="handleSubmit"
    >
      <a-form-item
        field="userAccount"
        label="账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        field="userPassword"
        label="密码"
        :rules="[{ required: true, message: '请输入密码' }]"
        tooltip="密码不少于8位"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item
        field="checkPassword"
        label="确认密码"
        :rules="[{ required: true, message: '请输入确认密码' }]"
        tooltip="密码不少于8位"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请输入确认密码"
        />
      </a-form-item>
      <a-form-item
        field="planetCode"
        label="编号"
        :rules="[{ required: true, message: '请输入编号' }]"
      >
        <a-input v-model="form.planetCode" placeholder="请输入编号" />
      </a-form-item>
      <a-form-item>
        <a-button html-type="submit" type="primary">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { userLogin, userRegister } from "@/api/user";
import { useLoginUserStore } from "@/store/useLoginUserStore";
import { useRouter } from "vue-router";
import message from "@arco-design/web-vue/es/message";

const loginUserStore = useLoginUserStore();

const router = useRouter();

interface UserLoginRequest {
  userAccount: string;
  userPassword: string;
  checkPassword: string;
  planetCode: string;
}

const form = ref<UserLoginRequest>({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
  planetCode: "",
});

const handleSubmit = async () => {
  //判断两次输入的密码是否相同
  if (form.value.userPassword != form.value.checkPassword) {
    message.error("两次输入的密码不一致");
  }
  const res = await userRegister(form.value);
  if (res.data.code === 0 && res.data.data) {
    message.success("注册成功");
    router.push({
      path: "/user/login",
      replace: true,
    });
  } else {
    message.error("注册失败" + res.data.description);
  }
};
</script>
<style scoped>
#userRegisterPage {
  text-align: center;
}
</style>
